(() => {

'use strict'; 
goog.require('Blockly.Arduino');
goog.require('Mixly.Boards');

const { Boards } = Mixly;
const BOARDS_TYPE = Boards.getType();

// 初始化QMC5883L电子罗盘模块
switch (BOARDS_TYPE) {
    case 'arduino_avr':
        Blockly.Arduino.qmc5883l_init = function() {
            var name = this.getFieldValue('NAME');
            var dropdownType = this.getFieldValue('TYPE');
            if (this.getInput('SDA') && this.getInput('SCL')) {
                Blockly.Arduino.definitions_['include_QMC5883LCompassSoft'] = '#include <QMC5883LCompassSoft.h>';
                let sda = Blockly.Arduino.valueToCode(this, 'SDA', Blockly.Arduino.ORDER_ATOMIC);
                let scl = Blockly.Arduino.valueToCode(this, 'SCL', Blockly.Arduino.ORDER_ATOMIC);
                Blockly.Arduino.definitions_['include_SoftwareWire'] = '#include <SoftwareWire.h>';
                Blockly.Arduino.definitions_[`var_declare_softWire${sda}_${scl}`] = `SoftwareWire softWire${sda}_${scl}(${sda}, ${scl});`;
                Blockly.Arduino.definitions_[`var_declare_${name}`] = `QMC5883LCompassSoft ${name}(&softWire${sda}_${scl});`;
            } else {
                Blockly.Arduino.definitions_['include_QMC5883LCompass'] = '#include <QMC5883LCompass.h>';
                Blockly.Arduino.definitions_['include_Wire'] = '#include <Wire.h>';
                Blockly.Arduino.definitions_[`var_declare_${name}`] = `QMC5883LCompass ${name}(&Wire);`;
            }
            Blockly.Arduino.setups_[`setup_compass_${name}`] =  `${name}.init();`;
            var code = '';
            return code;
        };
        break;
    default:
        Blockly.Arduino.qmc5883l_init = function() {
            var sda = Blockly.Arduino.valueToCode(this, 'SDA', Blockly.Arduino.ORDER_ATOMIC);
            var scl = Blockly.Arduino.valueToCode(this, 'SCL', Blockly.Arduino.ORDER_ATOMIC);
            var name = this.getFieldValue('NAME');
            var dropdownType = this.getFieldValue('TYPE');
            Blockly.Arduino.definitions_['include_QMC5883LCompass'] = '#include <QMC5883LCompass.h>';
            Blockly.Arduino.definitions_['include_Wire'] = '#include <Wire.h>';
            Blockly.Arduino.definitions_[`var_declare_${name}`] = `QMC5883LCompass ${name}(&Wire);`;
            Blockly.Arduino.setups_[`setup_compass_${name}`] =  `${name}.init(${sda}, ${scl});`;
            var code = '';
            return code;
        };
}

// QMC5883L电子罗盘模块刷新数据
Blockly.Arduino.qmc5883l_read = function() {
    var name = this.getFieldValue('NAME');
    var code = `${name}.read();\n`;
    return code;
};

// QMC5883L电子罗盘模块获取方位角
Blockly.Arduino.qmc5883l_get_azimuth = function() {
    var name = this.getFieldValue('NAME');
    var code =  `${name}.getAzimuth()`;
    return [code, Blockly.Arduino.ORDER_ATOMIC];
};

})();